package com.chaoxin.lovehelp.controller;

import com.chaoxin.lovehelp.common.utils.OSSClientUtil;
import com.chaoxin.lovehelp.common.utils.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author TengJay Huang
 * @version 1.0
 */
@RestController
@RequestMapping("/images")
public class UploadController {
    @PostMapping("/upload")
    public R fileupload(MultipartFile file, HttpServletRequest req) throws IOException {
        if (file == null || file.getSize() <= 0) {
            throw new IOException("file不能为空");
        }
        //获取文件的大小,单位/KB
        long size = file.getSize();
        OSSClientUtil ossClient=new OSSClientUtil();
        //将文件上传
        String name = ossClient.uploadImg2Oss(file);
        //获取文件的URl地址  以便前台  显示
        String imgUrl = ossClient.getImgUrl(name);
        HashMap<String, Object> map=new HashMap<>();
        //文件存储的路径
        map.put("imgurl", imgUrl);
        System.out.println("添加数据");
        return R.ok().setData(map);

    }
}
